#!/usr/bin/python
# -*- coding: utf-8 -*-

# --------------------------------------
# VIDEO BUDDHA - edit options window
# --------------------------------------
# Ranames files based on a specific regex pattern in
#  show_list.db and based on a specific list of file
#  extensions.
# --------------------------------------

import wx

import models.database


class WinEditOptions(wx.Frame):

	def __init__(self):
		wx.Frame.__init__(self, wx.GetApp().TopWindow, wx.ID_ANY, u'Options', wx.DefaultPosition, 
			wx.Size(547, 445), wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)

		# Database
		self.db = models.database.Database()
		
		# Application icon
		favicon = wx.Icon('res/favicon.ico', wx.BITMAP_TYPE_ICO, 16, 16)
		self.SetIcon(favicon)
		
		self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)

		sizer_top = wx.BoxSizer(wx.VERTICAL)

		self.panel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
		sizer_main = wx.BoxSizer(wx.VERTICAL)

		# General options sub sizer
		sbsizer_general = wx.StaticBoxSizer(wx.StaticBox(self.panel, wx.ID_ANY, u"General"), wx.HORIZONTAL)
		
		# General options column 1 sizer
		sizer_general_1 = wx.BoxSizer(wx.VERTICAL)

		self.check_remember_dir = wx.CheckBox(self.panel, wx.ID_ANY,
			u'Remember directory from prior session', wx.DefaultPosition, wx.DefaultSize, 0)		
		self.check_remember_dir.SetValue(True)
		sizer_general_1.Add(self.check_remember_dir, 0, wx.ALL, 5)

		self.check_log = wx.CheckBox(self.panel, wx.ID_ANY,
			u'Turn on logging to record renamed files', wx.DefaultPosition, wx.DefaultSize, 0)		
		sizer_general_1.Add(self.check_log, 0, wx.ALL, 5)
		
		sbsizer_general.Add(sizer_general_1, 1, wx.EXPAND, 5)
		
		# General options column 2 sizer
		sizer_general_2 = wx.BoxSizer(wx.VERTICAL)
		
		self.check_write_nfo = wx.CheckBox(self.panel, wx.ID_ANY, 
			u"Write NFO files for XBMC scraping", wx.DefaultPosition, wx.DefaultSize, 0)
		sizer_general_2.Add(self.check_write_nfo, 0, wx.ALL, 5)
		
		sbsizer_general.Add(sizer_general_2, 1, wx.EXPAND, 5)
		
		sizer_main.Add(sbsizer_general, 0, wx.ALL | wx.EXPAND, 5)
		
		# Video extensions
		sbsizer_video_exts = wx.StaticBoxSizer(wx.StaticBox(self.panel, wx.ID_ANY, u"Video Extensions"), 
                        wx.VERTICAL)
		self.m_staticText4 = wx.StaticText(self.panel, wx.ID_ANY,
			u'Search for video files with these extensions:',
        		wx.DefaultPosition, wx.DefaultSize, 0)
		self.m_staticText4.Wrap(-1)
		sbsizer_video_exts.Add(self.m_staticText4, 0, wx.ALL, 5)
			
		self.text_ctrl_extensions = wx.TextCtrl(self.panel, wx.ID_ANY,
			u'.avi .mpg .mp4 .mkv', wx.DefaultPosition, wx.DefaultSize, 0)	
		sbsizer_video_exts.Add(self.text_ctrl_extensions, 0, wx.ALL | wx.EXPAND, 5)

		sizer_main.Add(sbsizer_video_exts, 0, wx.ALL | wx.EXPAND, 5)

		# Renaming options
		sbsizer_renaming = wx.StaticBoxSizer(wx.StaticBox(self.panel,
			wx.ID_ANY, u'Renaming'), wx.HORIZONTAL)
		
		# Renaming -> General sizer
		sbsizer_renaming_general = wx.StaticBoxSizer(wx.StaticBox(self.panel, wx.ID_ANY, u"General"), wx.VERTICAL)
		self.m_staticText2 = wx.StaticText(self.panel, wx.ID_ANY,
			u'Enter the naming convention for your files using \nthe following variables:\n\n%t - Title\n%se - Season and episode\n%i - Trailing information',
        		wx.DefaultPosition, wx.DefaultSize, 0)
		
		self.m_staticText2.Wrap(-1)
		sbsizer_renaming_general.Add(self.m_staticText2, 0, wx.ALL, 5)

		self.text_ctrl_renaming_scheme = wx.TextCtrl(self.panel, wx.ID_ANY,
			u'%t - %se - %i', wx.DefaultPosition, wx.DefaultSize, 0)
		
		sbsizer_renaming_general.Add(self.text_ctrl_renaming_scheme, 0, wx.ALL | wx.EXPAND, 5)
		
		self.text_ctrl_renaming_ex = wx.TextCtrl(self.panel, wx.ID_ANY, u"Ex:", wx.DefaultPosition, 
                        wx.DefaultSize, 0 | wx.NO_BORDER)
		self.text_ctrl_renaming_ex.Enable(False)
		sbsizer_renaming_general.Add(self.text_ctrl_renaming_ex, 0, wx.ALL | wx.EXPAND, 5)

		sbsizer_renaming.Add(sbsizer_renaming_general, 1, wx.ALL | wx.EXPAND, 5)

		# Renaming -> Seasep Sizer
		sbsizer_seasep_format = wx.StaticBoxSizer(wx.StaticBox(self.panel, wx.ID_ANY, u'Season Episode'), wx.VERTICAL)

		self.m_staticText1 = wx.StaticText(self.panel, wx.ID_ANY,
			u'Select the format to use when writing the\nseason and episode information:',
	   		wx.DefaultPosition, wx.DefaultSize, 0)
		
		self.m_staticText1.Wrap(-1)
		sbsizer_seasep_format.Add(self.m_staticText1, 0, wx.ALL, 5)

		# Radio buttons
		self.radio_seasep_format_1 = wx.RadioButton(self.panel, wx.ID_ANY,
			u's01e01', wx.DefaultPosition, wx.DefaultSize, wx.RB_GROUP)
		self.radio_seasep_format_2 = wx.RadioButton(self.panel, wx.ID_ANY,
			u'S01E01', wx.DefaultPosition, wx.DefaultSize, 0)
		self.radio_seasep_format_3 = wx.RadioButton(self.panel, wx.ID_ANY,
			u'01x01', wx.DefaultPosition, wx.DefaultSize, 0)
		self.radio_seasep_format_4 = wx.RadioButton(self.panel, wx.ID_ANY,
			u'101', wx.DefaultPosition, wx.DefaultSize, 0)
			
		sbsizer_seasep_format.Add(self.radio_seasep_format_1, 0, wx.ALL, 5)
		sbsizer_seasep_format.Add(self.radio_seasep_format_2, 0, wx.ALL, 5)
		sbsizer_seasep_format.Add(self.radio_seasep_format_3, 0, wx.ALL, 5)
		sbsizer_seasep_format.Add(self.radio_seasep_format_4, 0, wx.ALL, 5)

		sbsizer_renaming.Add(sbsizer_seasep_format, 1, wx.ALL | wx.EXPAND, 5)

		sizer_main.Add(sbsizer_renaming, 0, wx.ALL | wx.EXPAND, 5)

		sizer_buttons = wx.BoxSizer(wx.HORIZONTAL)

		# Buttons save and cancel
		self.button_save = wx.Button(self.panel, wx.ID_ANY, u'Save', wx.DefaultPosition, wx.DefaultSize, 0)	
		sizer_buttons.Add(self.button_save, 1, wx.ALL | wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)

		self.button_cancel = wx.Button(self.panel, wx.ID_ANY, u'Cancel', wx.DefaultPosition, wx.DefaultSize, 0)		
		sizer_buttons.Add(self.button_cancel, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

		sizer_main.Add(sizer_buttons, 1, wx.EXPAND, 5)

		self.panel.SetSizer(sizer_main)
		self.panel.Layout()
		sizer_main.Fit(self.panel)
		sizer_top.Add(self.panel, 1, wx.EXPAND | wx.ALL, 0)

		self.SetSizer(sizer_top)
		self.Layout()

		self.Centre(wx.BOTH)

		# Set the options in accordance with the config database info
		if int(self.db.get_option('b_remember_dir')[1]) == 1:
			self.check_remember_dir.SetValue(True)
		else:
			self.check_remember_dir.SetValue(False)

		if int(self.db.get_option('b_log')[1]) == 1:
			self.check_log.SetValue(True)
		else:
			self.check_log.SetValue(False)
			
		if int(self.db.get_option('b_write_nfo')[1]) == 1:
			self.check_write_nfo.SetValue(True)
		else:
			self.check_write_nfo.SetValue(False)
		
		int_format = int(self.db.get_option('seasep_format')[1])
		if int_format == 1:
			self.radio_seasep_format_1.SetValue(True)
		elif int_format == 2:
			self.radio_seasep_format_2.SetValue(True)
		elif int_format == 3:
			self.radio_seasep_format_3.SetValue(True)
		elif int_format == 4:
			self.radio_seasep_format_4.SetValue(True)
		else:
			self.radio_seasep_format_1.SetValue(True)
			
		renaming_scheme = self.db.get_option('renaming_scheme')[1]
		self.text_ctrl_renaming_scheme.SetValue(renaming_scheme)
		
		video_extensions = self.db.get_option('video_extensions')[1]
		self.text_ctrl_extensions.SetValue(video_extensions)
		
		# Connect Events
		self.check_remember_dir.Bind(wx.EVT_CHECKBOX, self.on_remember_dir)
		self.check_log.Bind(wx.EVT_CHECKBOX, self.on_log)
		self.check_write_nfo.Bind(wx.EVT_CHECKBOX, self.on_write_nfo)
		self.button_save.Bind(wx.EVT_BUTTON, self.on_save)
		self.button_cancel.Bind(wx.EVT_BUTTON, self.on_cancel)
		self.Bind(wx.EVT_TEXT, self.on_text_ctrl_renaming, self.text_ctrl_renaming_scheme)
		self.Bind(wx.EVT_TEXT, self.on_text_ctrl_extensions, self.text_ctrl_extensions)
		
		# Radio events
		for radio in [self.radio_seasep_format_1, self.radio_seasep_format_2, self.radio_seasep_format_3, self.radio_seasep_format_4]:
			self.Bind(wx.EVT_RADIOBUTTON, self.on_radio, radio)
		
		# Set the intial example renaming scheme
		self.update_text_ctrl_renaming()

	def __del__(self):
		self.Close()

	def on_remember_dir(self, event):
		if self.check_remember_dir.IsChecked():
			self.db.update_option('b_remember_dir', 1)
		else:
			self.db.update_option('b_remember_dir', 0)

	def on_log(self, event):
		if self.check_log.IsChecked():
			self.db.update_option('b_log', 1)
		else:
			self.db.update_option('b_log', 0)
			
	def on_write_nfo(self, event):
		if self.check_write_nfo.IsChecked():
			self.db.update_option('b_write_nfo', 1)
		else:
			self.db.update_option('b_write_nfo', 0)
			
	def on_text_ctrl_renaming(self, event):
		self.update_text_ctrl_renaming()
		self.db.update_option('renaming_scheme', self.text_ctrl_renaming_scheme.GetValue())
		
	def on_text_ctrl_extensions(self, event):
		self.db.update_option('video_extensions', self.text_ctrl_extensions.GetValue())
		
	def update_text_ctrl_renaming(self):
		format = self.text_ctrl_renaming_scheme.GetValue()
		format = format.replace('%t', 'Top Gear')
		
		if self.radio_seasep_format_1.GetValue():
			format = format.replace('%se', 's01e01')
		elif self.radio_seasep_format_2.GetValue():
			format = format.replace('%se', 'S01E01')
		elif self.radio_seasep_format_3.GetValue():
			format = format.replace('%se', '01x01')
		elif self.radio_seasep_format_4.GetValue():
			format = format.replace('%se', '101')
		else:
			format = format.replace('%se', 's01e01')
			
		format = format.replace('%i', 'HDTV.720')
		format = format + '.avi'
		
		self.text_ctrl_renaming_ex.SetValue("Ex: " + format)
			
	def on_radio(self, event):
		radio_selected_id = event.GetEventObject().GetId()
		radio_1_id = self.radio_seasep_format_1.GetId()
		radio_2_id = self.radio_seasep_format_2.GetId()
		radio_3_id = self.radio_seasep_format_3.GetId()
		radio_4_id = self.radio_seasep_format_4.GetId()
		
		if radio_selected_id == radio_1_id:
			self.db.update_option('seasep_format', 1)
		elif radio_selected_id == radio_2_id:
			self.db.update_option('seasep_format', 2)
		elif radio_selected_id == radio_3_id:
			self.db.update_option('seasep_format', 3)
		elif radio_selected_id == radio_4_id:
			self.db.update_option('seasep_format', 4)
		else:
			self.db.update_option('seasep_format', 1)
			
		self.update_text_ctrl_renaming()

	def on_save(self, event):
		self.Close()

	def on_cancel(self, event):
		self.Close()
